package cn.codingjc.provider.service.impl;

import cn.codingjc.common.dto.DeptDTO;
import cn.codingjc.common.util.DeepBeanUtils;
import cn.codingjc.provider.dao.IDeptDao;
import cn.codingjc.provider.vo.Dept;
import cn.codingjc.service.IDeptService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author codingjc
 * @date 2022-07-02 14:49
 */

@Slf4j
@Service
public class DeptServiceImpl implements IDeptService {

    @Autowired
    private IDeptDao deptDao;

    @Override
    public DeptDTO get(long id) {
        DeptDTO deptDTO = new DeptDTO();
        BeanUtils.copyProperties(deptDao.selectById(id), deptDTO);
        return deptDTO;
    }

    @Override
    public boolean add(DeptDTO dto) {
        Dept dept = new Dept();
        BeanUtils.copyProperties(dto, dept);
        return deptDao.insert(dept) > 0;
    }

    @Override
    public List<DeptDTO> list() {
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        return DeepBeanUtils.copyListProperties(deptDao.selectList(queryWrapper), DeptDTO::new);
    }

    @Override
    public Map<String, Object> split(int currentPage, int lineSize, String colum, String keyword) {
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(colum, keyword);
        // 统计个数
        Integer count = this.deptDao.selectCount(queryWrapper);
        Page<Dept> page = this.deptDao.selectPage(new Page<>(currentPage, lineSize, count), queryWrapper);
        HashMap<String, Object> result = new HashMap<>();
        result.put("allDepts", DeepBeanUtils.copyListProperties(page.getRecords(), DeptDTO::new));
        result.put("allRecorders", page.getTotal());
        result.put("allPages", page.getPages());
        return result;
    }
}
